﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using SpLocalizer.Common;
using Microsoft.SharePoint;
using System.Xml;

namespace SpLocalizer.ContentSourceProviders.SpListItems
{
    internal abstract class  SpListItemAbstractValueLocalizationTask : BaseTranslationTask
    {


        #region Constants

        private const string Property_FieldId = "FieldId";
        private const string Property_ItemId = "ItemId";

        #endregion

        /// <summary>
        /// Respource key 
        /// </summary>
        public Guid FieldId
        {
            get { return new Guid(this.GetPropertyValue(Property_FieldId)); }
            set { this.SetPropertyValue(Property_FieldId, value.ToString()); }
        }


        /// <summary>
        /// Respource key 
        /// </summary>
        public Guid ItemId
        {
            get { return new Guid(this.GetPropertyValue(Property_ItemId)); }
            set { this.SetPropertyValue(Property_ItemId, value.ToString()); }
        }

        /// <summary>
        /// Return set of tasks for translation 
        /// </summary>
        /// <param name="FromLanguage"></param>
        /// <param name="ToLanguage"></param>
        /// <param name="item"></param>
        /// <param name="FieldId"></param>
        /// <returns></returns>
        internal virtual List<BaseTranslationTask> 
                    BuildTranslationTasks(CultureInfo FromLanguage, CultureInfo ToLanguage, 
            SPListItem item, Guid FieldId) 
        {

            List<BaseTranslationTask> returnValue = new List<BaseTranslationTask>();

            
            this.ItemId = item.UniqueId;
            this.FieldId = FieldId;
            this.SourceText = GetFieldTextToTranslate(item);
            this.Status = TranslationTaskStatus.Initialized;
            
            this.FromLanguage = FromLanguage;
            this.ToLanguage = ToLanguage;

            if (!string.IsNullOrEmpty(this.SourceText) && !IsGuidValue(this.SourceText))
            {
                returnValue.Add(this);
            }

            return returnValue;
        }


        protected abstract string GetFieldTextToTranslate(SPListItem item);


        internal abstract void SetFieldTextValue(SPListItem item);

        protected static bool IsGuidValue(string Value){
           try{
                Guid test = new Guid(Value);
                return true;
           }catch{
               return false;
           }
        }
    }
}
